package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.againstsky.verificationcode.VerificationCodeView;
import com.blankj.utilcode.util.ToastUtils;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.SucessBean;
import com.zsyh.jiujiu.bean.YanzhengCodeBean;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.TimeTextLogin;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class forgePassswardActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.yanzheng)
    EditText yanzheng;
    @BindView(R.id.code_view)
    VerificationCodeView codeView;
    @BindView(R.id.btn_get_code)
    Button btnGetCode;
    @BindView(R.id.re_yanzhen)
    RelativeLayout reYanzhen;
    @BindView(R.id.registershoujiyan)
    EditText registershoujiyan;
    @BindView(R.id.xinpassward)
    EditText xinpassward;
    @BindView(R.id.queren)
    EditText queren;
    @BindView(R.id.queding)
    TextView queding;
    private String yanCode;

    private TimeTextLogin mTimeText;

    @Override
    protected int initLayout() {
        return R.layout.activity_fogrt_passwoard;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        titleBar.getLeftLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thisActivity.finish();
            }
        });

        btnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(EmptyUtil.isEmpty(phone.getText().toString())){
                    ToastUtils.showShort("手机号不能为空");
                    return;
                }else if(isPhoneNumber(phone.getText().toString())==false){
                    ToastUtils.showShort("手机号不正确");
                    return;
                }

                if(EmptyUtil.isEmpty(yanzheng.getText().toString())){
                    ToastUtils.showShort("图形验证码不能为空");
                    return;
                }

                if(yanzheng.getText().toString().toLowerCase().equals(codeView.getVerificationCode().toLowerCase())){
                    Yzm(phone.getText().toString());
                }else{
                    ToastUtils.showShort("图形验证码不正确");
                }
            }
        });
    }

    @Override
    protected void initEvent() {

    }



    @OnClick(R.id.queding)
    public void onViewClicked() {

        if(EmptyUtil.isEmpty(phone.getText().toString())){
            ToastUtils.showShort("手机号不能为空");
            return;
        }else if(isPhoneNumber(phone.getText().toString())==false){
            ToastUtils.showShort("手机号不正确");
            return;
        }

        if(EmptyUtil.isEmpty(yanzheng.getText().toString())){
            ToastUtils.showShort("图形验证码不能为空");
            return;
        }

        if(EmptyUtil.isEmpty(registershoujiyan.getText().toString())){
            ToastUtils.showShort("手机验证码不能为空");
            return;
        }else if(!yanCode.equals(registershoujiyan.getText().toString())){
            ToastUtils.showShort("手机验证码不正确");
            return;
        }

        if(EmptyUtil.isEmpty(xinpassward.getText().toString())){
            ToastUtils.showShort("新密码不能为空");
            return;
        }

        if(EmptyUtil.isEmpty(queren.getText().toString())){
            ToastUtils.showShort("确认密码不能为空");
            return;
        }

        if(xinpassward.getText().toString().equals(queren.getText().toString())){
            findmima(phone.getText().toString(),xinpassward.getText().toString());
        }else{
            ToastUtils.showShort("新密码和确认密码不一致");
            return;
        }


    }

    /**
     * 验证码
     */

    public void Yzm(String phone){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .sendTextMsg(phone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("yanzgengma",JsonStr+"");
                        Gson gson=new Gson();
                        YanzhengCodeBean zhuCeBean=gson.fromJson(JsonStr,YanzhengCodeBean.class);
                        mTimeText = new TimeTextLogin(60000, 1000, btnGetCode);
                        mTimeText.start();
                        yanCode=zhuCeBean.getData();


                    }
                });
    }


    /**
     * 判断电话次数
     */
    public void findmima(String phone,String passward){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .findPassword( phone,passward)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, false) {
                    @Override
                    protected void onError(String errorMsg) {
                        Log.e("numbercallerror", errorMsg);
                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("numbercall", JsonStr);
                        Gson gson=new Gson();
                        SucessBean sucessBean=gson.fromJson(JsonStr,SucessBean.class);
                        if(sucessBean.getCode()==0){

                            thisActivity.finish();
                        }else{
                           ToastUtils.showShort(sucessBean.getMsg());
                        }


                    }
                });
    }
}
